package com.google.code.dweibo.core.user;

import java.util.ArrayList;
import java.util.List;

import weibo4j.Account;
import weibo4j.Friendships;
import weibo4j.Users;
import weibo4j.model.User;

import com.google.code.dweibo.core.auth.LoginService;
import com.google.code.dweibo.core.auth.LoginType;
import com.google.code.dweibo.util.WeiboServiceFactory;

public class SinaUserViewService{
	private LoginService loginService;

	public SinaUserViewService() throws Exception {
		loginService = WeiboServiceFactory.getLoginService(LoginType.SINA);
		if(!loginService.isLogin()){
			loginService.login(loginService.getPinCode());
		}
	}

	
	

	
	public  List<User> showFriends(String id) {
		// TODO Auto-generated method stub
		if (!loginService.isLogin()) {
			try {
				loginService.login(loginService.getPinCode());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			if (id == null) {
				id = getUID();
			}
			Friendships fm = new Friendships();
			List<User> userList = fm.getFriendsByID(id);
			return userList;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ArrayList<User>();
		}
	}
	

	public  List<User> showFollowers(String id) {
		// TODO Auto-generated method stub
		if (!loginService.isLogin()) {
			try {
				loginService.login(loginService.getPinCode());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			if (id == null) {
				id = getUID();
			}
			Friendships fm = new Friendships();
			List<User> userList = fm.getFollowersById(id);
			
			return userList;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ArrayList<User>();
		}
	}

	

	
	public String getUID() throws Exception {
		// TODO Auto-generated method stub
		Account am = new Account();
		return am.getUid().getString("uid");
	}

	
	public int getFollowerCount(String id) throws Exception {
		// TODO Auto-generated method stub
		if (id == null) {
			id = getUID();
		}
		return Users.showUserById(id).getFollowersCount();
	}

	
	public int getFriendsCount(String id) throws Exception {
		// TODO Auto-generated method stub
		if (id == null) {
			id = getUID();
		}
		return Users.showUserById(id).getFriendsCount();
	}
	
	

}
